<template>
	<view>
		<view class="navbar acea-row row-around">
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 'usable' }" @click="onNav('usable')">
				未使用</view>
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 'unusable' }"
				@click="onNav('unusable')">已使用</view>
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 'overdue' }"
				@click="onNav('overdue')">已过期</view>


		</view>

		<view class='coupon-list' v-if="couponsList.length">

			<view class='item coupon-list-msg acea-row ' :class='item.validStr=="usable"?"bj":"bj2" '
				v-for=' (item,index) in couponsList' :key="index">
				<view class="tagImg">
					<image :src="item.image" mode=""></image>
				</view>

				<view class='money'
					:class="item.validStr==='unusable'||item.validStr==='overdue'||item.validStr==='notStart' ? 'moneyGray' : ''">
					<view>￥<text class='num'>{{item.money?Number(item.money):''}}</text></view>
					<view class="pic-num">满{{ item.minPrice?Number(item.minPrice):'' }}元可用</view>
				</view>
				<view class='textss'>
					<view class='condition line1'>
						<!-- <span class="line-title"
							:class="item.validStr==='unusable'||item.validStr==='overdue'||item.validStr==='notStart' ? 'bg-color-huic' : 'bg-color-check'"
							v-if="item.useType === 1">通用</span>
						<span class="line-title"
							:class="item.validStr==='unusable'||item.validStr==='overdue'||item.validStr==='notStart' ? 'bg-color-huic' : 'bg-color-check'"
							v-else-if="item.useType === 2">商品</span>
						<span class="line-title"
							:class="item.validStr==='unusable'||item.validStr==='overdue'||item.validStr==='notStart' ? 'bg-color-huic' : 'bg-color-check'"
							v-else-if="item.useType === 3">品类</span> -->

						<span>{{item.name}}</span>
					</view>
					<view class='data'>
						<view>{{item.useStartTimeStr}}~{{item.useEndTimeStr}}</view>
						<view>使用范围：{{item.useRangeStr}}</view>
						<!-- <view style="color: $maincolor;">券码：1008611</view> -->
						<view class="" style="display: flex;justify-content: flex-end;">
							<view class='bnt' @click="goPage(item)"
								:class="item.validStr==='unusable'||item.validStr==='overdue'||item.validStr==='notStart'?'gray':'bg-color'">

								{{item.validStr | validStrFilter}}
							</view>
						</view>

					</view>
				</view>
			</view>
		</view>
		<view class='loadingicon acea-row row-center-wrapper' v-if="couponsList.length">
			<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
		</view>
		<view class='noCommodity' v-if="!couponsList.length">
			<view class='pictrue' style="text-align: center;">
				<image src="https://gfry.oss-cn-hangzhou.aliyuncs.com/crmebimage/public/content/2024/03/07/icon_nodata.png" mode=""></image>
				暂无数据
			</view>
		</view>
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
		<uni-popup ref="safeguardPup" type="center">
			<view class="" style="text-align: center;">
				<image :style="{width: `${qrcodeSize}px`, height: `${qrcodeSize}px`}" :src="qrcodeSrc" mode="aspectFit">
				</image>
				<canvas canvas-id="qrcode" v-if="isCode" style="margin: 0 auto;"
					:style="{width: `${qrcodeSize}px`, height: `${qrcodeSize}px`}" />
				
			</view>
			<view class="safeguardPup-msg-msg" @click="saveImg">点击保存</view>
		</uni-popup>
		<home></home>
	</view>
</template>

<script>
	import uQRCode from '@/js_sdk/Sansnn-uQRCode/uqrcode.js'
	import {
		getUserCoupons
	} from '@/api/api.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import home from '@/components/home';
	export default {
		components: {
			// #ifdef MP
			authorize,
			// #endif
			home

		},
		filters: {
			validStrFilter(status) {
				const statusMap = {
					'usable': '立即使用',
					'unusable': '已使用',
					'overdue': '已过期',
					'notStart': '未开始'
				}
				return statusMap[status]
			}
		},
		data() {
			return {
				isCode:false,
				qrcodeSrc: '',

				// qrcodeSize: 136,
				codeImg: '',

				qrcodeText: '',
				// 二维码尺寸
				qrcodeSize: 200,

				couponsList: [],
				loading: false,
				loadend: false,
				loadTitle: '加载更多', //提示语
				page: 1,
				limit: 20,
				navOn: 'usable',
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false //是否隐藏授权
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUseCoupons();
					}
				},
				deep: true
			}
		},
		onLoad() {

			if (this.isLogin) {
				this.getUseCoupons();
			} else {
				toLogin();
			}
		},
		methods: {
			saveImg() {
				let that = this

				uni.saveImageToPhotosAlbum({
					filePath: that.qrcodeSrc,
					success: function(e) {

						uni.showToast({
							title: "保存成功,请在手机相册中查看",
							icon: 'none',
							duration: 2000
						})
						console.log('save success');
					}
				});

			},
			getCode() {
				uQRCode.make({
					canvasId: 'qrcode',
					text: this.qrcodeText,
					size: this.qrcodeSize,
					margin: 10,
					success: res => {
						this.isCode=false
						this.qrcodeSrc = res
						console.log('qrcodeSrc = ' + this.qrcodeSrc);
					},
					complete: () => {
						uni.hideLoading()
					}
				})
			},
			goPage(item) {
				// console.log(this.isCode,'this.isCode')
				if (item.useType == 3) {
					this.isCode=true
					this.qrcodeText = item.verCode
					this.$refs.safeguardPup.open()

					this.getCode()
					console.log("我爱你")
					console.log(item, '斤斤计较')
				} else {
					if (item.validStr == "usable") {
						uni.navigateTo({
							url: `/pagesA/associatedCoupons/index?couponId=${item.id}`
						})
					}
				}


			},

			/**
			 * 授权回调
			 */
			onLoadFun: function() {
				this.getUseCoupons();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			onNav: function(type) {
				this.navOn = type;
				this.couponsList = [];
				this.page = 1;
				this.loadend = false;
				this.getUseCoupons();
			},
			/**
			 * 获取领取优惠券列表
			 */
			getUseCoupons: function() {
				let that = this;
				if (this.loadend) return false;
				if (this.loading) return false;
				getUserCoupons({
					page: that.page,
					limit: that.limit,
					type: that.navOn
				}).then(res => {
					let list = res.data ? res.data.list : [],
						loadend = list.length < that.limit;
					let couponsList = that.$util.SplitArray(list, that.couponsList);
					that.$set(that, 'couponsList', couponsList);
					that.loadend = loadend;
					that.loadTitle = loadend ? '我也是有底线的' : '加载更多';
					that.page = that.page + 1;
					that.loading = false;
				}).catch(err => {
					that.loading = false;
					that.loadTitle = '加载更多';
				});
			}
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			this.getUseCoupons();
		}
	}
</script>

<style lang="scss" scoped>
	.safeguardPup-msg-msg{
    width: 100%;
    color: #fff;
    border-radius: 60rpx;
    background: $maincolor;
    // font-size: 15px;
    margin-top: 100rpx;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    font-size: 32rpx;
	}
	.list {
		width: 80%;
		margin: 0 auto;
		background: red;
	}

	.coupon-list-msg {
		position: relative;

		.tagImg {
			position: absolute;
			width: 82rpx;
			height: 82rpx;
			right: 30px;
			bottom: 25%;
			border-radius: 50%;
			z-index: 10;

			image {
				border-radius: 50%;
				width: 100%;
				height: 100%;
			}
		}
	}

	.navbar {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 106rpx;
		background-color: #FFFFFF;
		z-index: 9;

		.item {
			border-top: 5rpx solid transparent;
			border-bottom: 5rpx solid transparent;
			font-size: 30rpx;
			color: #999999;

			&.on {
				border-bottom-color: $maincolor;
				color: #282828;
			}
		}
	}

	.money {
		display: flex;
		flex-direction: column;
		justify-content: center;
		color: #fff;

		width: 27%;
		align-items: center;
		// width: 30%;
	}

	.pic-num {
		color: #ffffff;
		font-size: 24rpx;
	}

	.coupon-list {
		margin-top: 122rpx;
	}

	.coupon-list .item {
		// background: #fff;

		height: 100% !important;
		padding: 30rpx 0;
		margin-bottom: 16rpx;
	}

	.coupon-list .item .text .data {
		height: 100% !important;
	}

	.coupon-list .item .textss {
		height: 100%;
		width: 70% !important;

		background: none !important;
		margin-left: 20rpx;
		padding-left: 20rpx;
		padding-right: 20rpx;

		.data {
			font-size: 20rpx;
			line-height: 43rpx;

			.bnt {
				width: 144rpx;
				// background: $maincolor;
				color: #fff;
				text-align: center;
				border-radius: 100rpx;
				padding: 7rpx 0;
				font-size: 22rpx;
			}
		}

		.condition {
			font-size: 28rpx;
		}
	}

	.coupon-list .item .text .condition {
		/* display: flex;
		align-items: center; */
	}

	.condition .line-title {
		width: 90rpx;
		height: 40rpx !important;
		line-height: 40rpx !important;
		padding: 2rpx 10rpx;
		-webkit-box-sizing: border-box;
		box-sizing: border-box;
		background: rgba(255, 247, 247, 1);
		border: 1px solid $maincolor;
		opacity: 1;
		border-radius: 20rpx;
		font-size: 18rpx !important;
		color: $maincolor;
		margin-right: 12rpx;
	}

	.noCommodity {
		margin-top: 300rpx;
	}

	.bj2 {
		background-image: url("");
		background-size: 120% 100%;
	}

	.bj {
		background-image: url('');
		// background-size: 100% 100;
		background-size: 120% 100%;
	}
</style>